﻿#--------------------------------------------------------------------------------------------------------------------------------------------------------
#
# Mi-DevEnv			:	http://midevenv.codeplex.com 
#					:	(c) IT Bridge 2012
#					:	A concept originated by Chris Nurse
#
# Licence			:	GNU General Public License version 2 (GPLv2)
#					:	http://midevenv.codeplex.com/license
#
# Summary           :   Automatically trigger package installation according to modules listed in the BuildPackages files
#
#--------------------------------------------------------------------------------------------------------------------------------------------------------

Set-StrictMode -Version Latest
Remove-Module *
Import-Module MDE-Common

$package = $null
$module = $null
$description = $null
$Name = $null
$ServerBuild = $null
$AlreadyInstalled = $null

try {

    [string]$executionPath = Split-Path $script:MyInvocation.MyCommand.Path -Parent
    Set-ContinueAfterReboot -DomainName "MDE_DOMAIN_NAME" -User "Administrator" -Password "MDE_DEFAULT_PASSWORD" -Path $script:MyInvocation.MyCommand.Path

    Write-Header "Package Installer"

    # Read-Host "Press enter to being"

    if (Test-Path "$executionPath\BuildPackages.csv") {

        $RequiredPackages = Import-Csv $executionPath\BuildPackages.csv
        $Packages = Get-InstallPackages -Path $executionPath -RequiredPackages $RequiredPackages

        Write-Host "Reading installed software"
        $InstalledPackages = Get-InstalledSoftware

        $Name = Get-ComputerName
        Set-Content -Path "\\vmhost\installfiles\Transfer\Installed-$Name.txt" -Value $InstalledPackages

        foreach($RequiredPackage in $RequiredPackages) {
     
            try {
                $package = $Packages[$RequiredPackage.Package]
                $module = $package.Module
                $description = $module.GetPackageDescription()
                $Name = $module.GetInstallName()
                $ServerBuild = $module.GetServerBuild()
                }
            catch {
                Write-Progress ("Can't get all module information for " + $RequiredPackage.Package)
                throw ("Can't get all module information for " + $RequiredPackage.Package)
                }

            # Software package installers can be disregard if the package is already installed
            # but server builds have to run logic to know if they've been installed or not
            if ($ServerBuild) {                    
                $AlreadyInstalled = $module.GetInstalled()
                }
            else {
                $AlreadyInstalled = ($InstalledPackages -contains $Name)
                }

            if (! $AlreadyInstalled ) {
        
                $date = Get-Date


                Write-Host "-------------------------------------------------------------" -ForegroundColor Cyan
                Write-Host ""
                Write-Host "Mi-DevEnv - Installer" -ForegroundColor Cyan
                Write-Host ""
                Write-Progress "Installation of $description commenced at $date" 
                Write-Host ""
                Write-Host "-------------------------------------------------------------" -ForegroundColor Cyan
                Write-Host ""

                $module.InstallProduct()

                Write-Progress "Installation of $description ended at $date" 

                if ($module.GetRequireRestart()) {
                    Restart-Machine
                    }

                }
     
            }
        }

    Write-Host ""
    Write-Host ""
    Write-Host ""
    Write-Host "Mi-DevEnv - Installer - End of batch installation" -ForegroundColor Cyan
    Write-Host "                      - Clearing auto-logon actions" -ForegroundColor Cyan
    
    Clear-State
    }
    catch {
        $log = "Package: {$package} | Module: $module | Description: $description | Name: $Name | ServerBuild: $ServerBuild | AlreadyInstalled: $AlreadyInstalled"
        Write-Progress ("Exception occured : " + $_.Exception.Message + " // $log")
    }